<?php

namespace CMBUBank\UBank\Response;

use CMBUBank\Http\Response;

class UBankBaseResponse extends Response
{
    protected $reqCode;

    protected $responseCode = [];

    protected $pageCode;

    protected $pageInfo;

    protected $nextPage = 0;

    protected $bussList = [];

    /**
     * 请求状态 (A.5 业务请求状态)
     * @var  $requestStatus
     */
    protected $requestStatus;

    /**
     * 业务处理结果 (A.6 业务处理结果)
     * @var  $returnFlg
     */
    protected $returnFlg;

    /**
     * 流程实例号
     * @var  $subProNo
     */
    protected $subProNo;

    protected $isFinish;


    private $errorMsg;

    private $errorCode;


    public function resolve(): Response
    {
        $body = $this->getBody();

        if (base64_decode($body)) {
            $this->setBody($body);
        } else {
            $this->setError($this->getBody());
        }
        return $this;
    }

    protected function format(): Response
    {
        if (json_decode($this->getBody())) {
            $body = json_decode($this->getBody(), true);

            $this->setBody($body);

            $code = $body['response']['head']['resultcode'] ?? '';

            if ($code !== 'SUC0000') {
                $errMsg = $body['response']['head']['resultmsg'] ?? '未知错误';
                $this->setError($errMsg);
                return $this;
            } else {

                if (count($this->responseCode)) {
                    foreach ($this->responseCode as $code) {
                        $t = $body['response']['body'][$code] ?? [];
                        if ($t) {
                            $this->bussList = $t;
                        }
                    }
                }

                //分页参数
                if ($this->pageCode) {
                    $this->pageInfo = $body['response']['body'][$this->pageCode] ?? [];
                }
            }

            //if (is_array($this->bussList) and count($this->bussList) == 1) {
            //    $this->bussList = array_shift($this->bussList);
            //}

            $this->pageFormat();

        }


        return $this;
    }

    protected function formatLimit1(): UBankBaseResponse
    {
        self::format();

        if (is_array($this->bussList) and count($this->bussList) == 1) {
            $this->bussList = array_shift($this->bussList);
        }

        $this->subCode = $this->get('dyanbr');
        $this->subProNo = $this->get('reqnbr');

        $this->statusFormat();

        $this->checkFinish();

        return $this;
    }

    protected function pageFormat()
    {

    }

    protected function statusFormat()
    {
        $this->requestStatus = $this->get('reqsts','');

        $this->returnFlg = $this->get('rtnflg','');
    }

    public function getNextPage()
    {
        return $this->nextPage;
    }

    /**
     * 获取 业务参考号
     * @return mixed
     */
    public function getReqCode()
    {
        return $this->reqCode;
    }

    public function setReqCode($reqCode): void
    {
        $this->reqCode = $reqCode;
    }

    public function get($string, $default = [])
    {
        return $this->bussList[$string] ?? $default;
    }

    public function getErrorMsg()
    {
        return $this->errorMsg;
    }

    public function getErrorCode()
    {
        return $this->errorCode;
    }

    public function getRequestStatus()
    {
        return $this->requestStatus;
    }

    public function getReturnFlg()
    {
        return $this->returnFlg;
    }

    /**
     * 获取 流程实例号
     * @return mixed
     */
    public function getSubProNo()
    {
        return $this->subProNo;
    }
}